<?php

namespace app\modules\prod\controllers;

use yii;
use yii\helpers\Json;
use app\common\Controller;
use app\common\SystemEvent;
use yii\web\ForbiddenHttpException;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;
use yii\web\Response;
use yii\widgets\ActiveForm;
use yii2mod\editable\EditableAction;
use yii2mod\rbac\filters\AccessControl;
use app\models\RegionType;
use yii\data\Pagination;

class RegionTypeController extends Controller
{
    
	public $module;
	public $protected_uids = [];    
	public $superadmin_uid = 0;
	public $loginRedirect = '/admin/dashboard';
	public $defaultAction = 'index';
	public function init()
	{
		parent::init();
	}


     public function actionIndex()
    {
        $query = RegionType::find()->alias('a');
        $pagination = new Pagination([
            'defaultPageSize' => 5,
            'totalCount' => $query->count(),
        ]);
        $regionTypes = $query->joinWith('parentRegionTypes  AS b')->orderBy('id')->offset($pagination->offset)->limit($pagination->limit)->all();
        return $this->render('index', ['pagination' => $pagination ,
        'regionTypes' => $regionTypes ]);

    }

    /**
     * Create a new User model. If creation is successful, the browser will
     * be redirected to the 'view' page.
     * @return mixed
     */
    public function actionCreate()
    {
        /** @var \amnah\yii2\user\models\User $user */
        /** @var \amnah\yii2\user\models\Profile $profile */

//        $user = $this->module->model("User");
//        $user->setScenario("admin");
//        $profile = $this->module->model("Profile");
//
//        $post = Yii::$app->request->post();
//        $userLoaded = $user->load($post);
//        $profile->load($post);
//
//        // validate for ajax request
//        if (Yii::$app->request->isAjax) {
//            Yii::$app->response->format = Response::FORMAT_JSON;
//            //return ActiveForm::validate($user, $profile);
//        }
//
//        if ($userLoaded && $user->validate() && $profile->validate()) {
//            $user->save(false);
//            $profile->setUser($user->id)->save(false);
//            //更新授权
//            Yii::$app->authManager->assign(Yii::$app->authManager->getRole($user->role->name), $user->id);
//            return $this->redirect(['index']);
//        }

        $model = new RegionType();

        return $this->render('create', ['model' => $model]);

        // render
      //  return $this->render('create');
    }

//    public function actionIndex()
//    {
//        $menus = SystemEvent::GetAdminMenu();
//        if(Yii::$app->request->isAjax){
//            return '<script>OA_Menus='.Json::encode($menus).'</script>';
//        }
//        return $this->renderPartial('index',['menus'=>$menus]);
//    }
}
